package com.example.demoback.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demoback.entity.LoginInfo;
import com.example.demoback.entity.User;
import com.example.demoback.mapper.UserMapper;
import com.example.demoback.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public LoginInfo login(String username, String password) throws ServiceException {
        if (username == null || username.isBlank()) {
            throw new ServiceException("请输入用户名！");
        }
        if (password == null || password.isBlank()) {
            throw new ServiceException("请输入密码！");
        }
        if (password.trim().length() < 6) {
            throw new ServiceException("密码长度至少是6个字符！");
        }

        User user = userMapper.selectByUsernameAndPassword(username, password);
        if (user == null) {
            throw new ServiceException("用户或密码错误！");
        }
        else{
            //将登录信息存储到hashmap中以生成jwt秘钥
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("username",user.getUsername());
            dataMap.put("password",user.getPassword());
            dataMap.put("id",user.getId());
            //生成秘钥
            String jwt= JwtUtils.generateJwt(dataMap);
            //构造返回值
            return new LoginInfo(user.getId(),user.getUsername(),user.getPassword(),jwt);
        }

    }


    public User getById(Integer id) {
        return userMapper.selectById(id);
    }

    public User register(User user) throws ServiceException {
        // 参数校验
        if (user.getUsername() == null || user.getUsername() .isBlank()) {
            throw new ServiceException("请输入用户名！");
        }
        if (user.getPassword() == null || user.getPassword().isBlank()) {
            throw new ServiceException("请输入密码！");
        }
        if (user.getNickname() == null || user.getNickname().isBlank()) {
            throw new ServiceException("请输入昵称！");
        }
        if (user.getSchool() == null || user.getSchool() .isBlank()) {
            throw new ServiceException("请输入学校！");
        }
        if (user.getPassword().trim().length() < 6) {
            throw new ServiceException("密码长度至少是6个字符！");
        }
        // 检查用户名是否已存在
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,user.getUsername());
        Long cnt = userMapper.selectCount(lqw);
        if (cnt > 0) {
            throw new ServiceException("用户名已经存在！");
        }
        // 插入用户数据
        userMapper.insertOrUpdate(user); // 使用 insert 方法插入新用户
        return user;
    }
    public User save(User user) throws ServiceException {
        // 参数校验
        if (user.getUsername() == null || user.getUsername() .isBlank()) {
            throw new ServiceException("请输入用户名！");
        }
        if (user.getPassword() == null || user.getPassword().isBlank()) {
            throw new ServiceException("请输入密码！");
        }
        if (user.getNickname() == null || user.getNickname().isBlank()) {
            throw new ServiceException("请输入昵称！");
        }
        if (user.getSchool() == null || user.getSchool() .isBlank()) {
            throw new ServiceException("请输入学校！");
        }
        if (user.getPassword().trim().length() < 6) {
            throw new ServiceException("密码长度至少是6个字符！");
        }
        // 检查用户名是否已存在
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,user.getUsername());
        Long cnt = userMapper.selectCount(lqw);
        if (cnt <0) {

            throw new ServiceException("保存出错！");
        }
        // 插入用户数据
        userMapper.insertOrUpdate(user); // 使用 insert 方法插入新用户
        return user;
    }
}